package com.indeep.user.config;

import com.indeep.common.emun.RedisKeys;
import com.indeep.user.service.impl.IUserInfoServiceImpl;
import com.indeep.user.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class StartupRunner {
    
    private final IUserInfoServiceImpl userInfoService;
    
    private final RedisTemplate redisTemplate;
    
    @Autowired
    public StartupRunner(IUserInfoServiceImpl userInfoService, RedisTemplate redisTemplate) {
        this.userInfoService = userInfoService;
        this.redisTemplate = redisTemplate;
    }
    
    @PostConstruct
    public void init() {
        // 把不需要发送短信验证码的用户放入redis设定不过期
        List<UserInfoVO> userInfoVOList = userInfoService.queryAllUserStatus();
        Map<String, String> allInitUser = userInfoVOList.stream()
                // 过滤出非空的priTempId，避免null值作为Map的键
                .filter(userInfoVO -> userInfoVO.getPriTempId() != null)
                // 使用Collectors.toMap收集器来收集结果，key和value都是priTempId
                .collect(Collectors.toMap(
                        // 作为Map的键的函数
                        UserInfoVO::getPriTempId,
                        // 作为Map的值的函数
                        UserInfoVO::getPriTempId
                ));
        System.out.println(userInfoVOList);
        redisTemplate.opsForHash().putAll(RedisKeys.JUMP_SEND_CODE, allInitUser);
    }
}